<?php
/**
 * Project: Recipe
 * File: RecipeArticleAction.class.php
 * Author: Xnfy520@gmail.com
 * Date: 12-5-4
 * Time: 上午11:34
 */


class RecipeArticleAction extends CommonAction{

	function index(){

		import("ORG.Util.Page");

		$recipecategory = M('RecipeCategory');
		$recipecategoryinfo = $recipecategory->field('id,name')->select();
		$this->assign('recipecategoryinfo', $recipecategoryinfo);

		$recipearticle = D('RecipeArticle');

		if(isset($_POST['search'])){
			if(!empty($_POST['key'])){
				switch($_POST['search']){
					case 1 :
						$datas['id'] = $_POST['key'];
						break;
					case 2:
						$datas['name'] = array("like","%{$_POST['key']}%");
						break;
					case 3 :
						$datas['alias'] = array('like',"%{$_POST['key']}%");
						break;
				}
				if(isset($_GET['cid'])){
					$datas['recipecategoryid'] = $_GET['cid'];
				}
				$total = $recipearticle->where($datas)->count();
				$Page = new Page($total, C('PAGENUM'));
				$show = $Page->show();
				$list = $recipearticle->relation(true)->where($datas)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}else{
				$this->error('搜索内容不能为空', __APP__.'/RecipeArticle/index');
			}
		}else{

			if(!isset($_GET['cid'])){
				$total = $recipearticle->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $recipearticle->relation(true)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}else{
				$total = $recipearticle->where('recipecategoryid='.$_GET['cid'])->count();
				$Page = new Page($total, C('PAGENUM'));// 实例化分页类 传入总记录数和每页显示的记录数
				$show = $Page->show();// 分页显示输出
				$list = $recipearticle->relation(true)->where('recipecategoryid='.$_GET['cid'])->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
			}

		}

		$this->assign('list',$list);// 赋值数据集

		$this->assign('show',$show);// 赋值分页输出

		$this->display('index');
	}

	/**
	 * 删除菜谱
	 */
	function del(){
		if(!empty($_POST['actions'])){
			$srcdir = './Public/Content/RecipeArticle/';

			$recipearticle= D('RecipeArticle');
			$recipenutritions = M('RecipeNutritions');

			$recipemain = M('RecipeMain');
			$recipeaccessory = M('RecipeAccessory');
			$recipecondiment = M('RecipeCondiment');

			$recipecomments = M('RecipeComments');
			foreach($_POST['actions'] as $key=>$value){
				$recipearticleinfo = $recipearticle->relation(true)->find($value);

				if(!empty($recipearticleinfo['image'])){
					unlink($srcdir.'Big_'.$recipearticleinfo['image']);
					unlink($srcdir.'Small_'.$recipearticleinfo['image']);
				}

				$delrecipe['recipetypeid'] = $recipearticleinfo['recipetypeid'];
				$delrecipe['recipecategoryid'] = $recipearticleinfo['recipecategoryid'];
				$delrecipe['id'] = $value;
				$num = $recipearticle->where($delrecipe)->delete($value);

				$deletes['recipetypeid'] = $recipearticleinfo['recipetypeid'];
				$deletes['recipecategoryid'] = $recipearticleinfo['recipecategoryid'];
				$deletes['recipearticleid'] = $value;
				$recipemain->where($deletes)->delete();
				$recipeaccessory->where($deletes)->delete();
				$recipecondiment->where($deletes)->delete();

				$del['sectionid'] = $recipearticleinfo['recipetypeid'];
				$del['categoryid'] = $recipearticleinfo['recipecategoryid'];
				$del['articleid'] = $value;
				$recipenutritions->where($del)->delete();
				$recipecomments->where('categoryid='.$recipearticleinfo['recipecategoryid'].' AND articleid='.$value)->delete();
			}
			if($num>0){
				$this->success('删除菜谱成功', __APP__.'/RecipeArticle/index/cid/'.$recipearticleinfo['recipecategoryid']);
			}else{
				$this->error('删除菜谱失败');
			}
		}else{
			$this->error('请先选择菜谱');
		}
	}

	function addrecommend(){
		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$recipe = M('RecipeArticle');
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				if($recipe->where($map)->setField('recommend',1)){
					$this->success('推荐成功');
				}else{
					$this->error('推荐失败');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function delrecommend(){
		if(isset($_GET['cid']) && isset($_GET['id'])){
			if(!empty($_GET['cid']) && !empty($_GET['id'])){
				$recipe = M('RecipeArticle');
				$map['recipecategoryid'] = $_GET['cid'];
				$map['id'] = $_GET['id'];
				if($recipe->where($map)->setField('recommend',0)){
					$this->success('取消推荐成功');
				}else{
					$this->error('取消推荐失败');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function returns(){
		$foodcategory = M('FoodCategory');
		$list = $foodcategory->field('id,name')->where('recipecategoryid='.$_POST['id'])->select();
		$data = array();
		foreach($list as $key=>$value){
			$data[] = $value;
		}
		echo json_encode($data);
	}

}